<!-- 表单 -->
<template>
    <div class="dyform">
        <!-- 表单头部 -->
        <form-header :elForm_Options="props.elForm_Options" @bottonHandle="opbuttonclick" :flagIndex="flagIndex" />
        <!-- 表单内容 -->
        <form-content :elForm_Options="props.elForm_Options" :flagIndex="flagIndex" @openBack="openBack"
            @successdeal="successdeal" ref="myForm" @changeinputNumber="changeinputNumber"
            @chooseDataBackHandle="chooseDataBackHandle" />
    </div>
</template>

<!-- 
    elForm_Options: 表单信息
    flagIndex: 页面状态(/新增/查看/编辑)
    opbuttonclick: 点击操作按钮回调
    changeinputNumber: 改变数字框回调
    chooseDataBackHandle: 选择框回调
    successdeal: 保存数据后的回调
-->

<script setup>
import { ref, nextTick } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import mittBus from '@/utils/mitt'

const route = useRoute()
const router = useRouter()

let props = defineProps({
    elForm_Options: {
        type: Object,
        default: () => {
            return {}
        }
    },
    flagIndex: {
        type: String,
        default: () => 'detail'
    }
})

let emits = defineEmits(['bottonHandle', 'openBack', 'successdeal', 'changeInputHandle', 'chooseDataBackHandle'])

//实例
let myForm = ref(null)

//按钮点击
let opbuttonclick = (wayname) => {
    emits('bottonHandle', wayname)
}

//获取表单数据
let getFormData = (params) => {
    nextTick(() => {
        myForm.value.getFormData(params)
    })
}

//获取表单后的回调
let openBack = (row) => {
    emits('openBack', row)
}

//提交表单 
let submitFormData = (params) => {
    nextTick(() => {
        myForm.value.submitFormData(params)
    })
}

//保存数据后的回调
let successdeal = (row) => {
    console.log(row)
    //如果需要回调就触发
    if (props.elForm_Options.needSuccessCb) {
        emits('successdeal', row)
        return
    }
    //不需要内部直接走
    if (props.flagIndex == 'detail') {
        getFormData({ id: route.params.id })
    } else {
        //新增用户不同
        if (props.elForm_Options.getwayname == 'users') {
            router.push({
                path: '/systemManage/userManagement'
            })
            return
        }
        //查看页
        let pathList = route.fullPath.split('/')
        pathList.pop()
        if (props.flagIndex == 'edit') {
            pathList.pop()
        }
        let path = pathList.join('/') + '/view/' + row.res.id
        router.push({
            path
        })
    }
}

//设置表单数据
let setFormData = (formData) => {
    myForm.value.setFormData(formData)
}

//关闭tagView
let closeCurrentTagsView = () => {
    mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 1, ...route }));
}

//改变数字框回调
let changeinputNumber = (currentValue, item, ruleForm) => {
    emits('changeInputHandle', currentValue, item, ruleForm)
}

//弹框选择数据后的回调
let chooseDataBackHandle = (prop, selection, ruleForm) => {
    emits('chooseDataBackHandle', prop, selection, ruleForm)
}

defineExpose({ getFormData, submitFormData, setFormData, closeCurrentTagsView })
</script>

<style lang="scss" scoped>
.dyform {
    background-color: #fff;
    border-radius: 5px;
    padding-bottom: 10px;
}
</style>